package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.entity.realTime.EntityNewUserAddCashUserByHour;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 实时数据-新增账号付费人数manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class NewUserAddCashUserByHourManager extends ManagerQueryCacheBase<EntityNewUserAddCashUserByHour> {
	private static final long serialVersionUID = 1L;
	
	public List<EntityNewUserAddCashUserByHour> getChannelHourNewUserAddCashUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityNewUserAddCashUserByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid !=:channelid and serverid=:serverid order by hour desc ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	public List<EntityNewUserAddCashUserByHour> getHourNewUserAddCashUserByDate(Date date, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("from EntityNewUserAddCashUserByHour where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid order by hour ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer);
	}
	
	// 根据平台、渠道、区服更新小时付费金额数据
	public void updateHourNewUserAddCashUserByConditions(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer) {
		List<EntityNewUserAddCashUserByHour> ooLookDate = getHourNewUserAddCashUserByDate(lookDate, queryPlatform, queryChannel, queryServer);
		List<EntityNewUserAddCashUserByHour> ooCompareDate = getHourNewUserAddCashUserByDate(compareDate, queryPlatform, queryChannel, queryServer);
        List<EntityNewUserAddCashUserByHour> ooLastWeekDate = getHourNewUserAddCashUserByDate(lastWeekDate, queryPlatform, queryChannel, queryServer);
        if ("1".equals(updateType)) {  // 分时
            updateData(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        } else if ("2".equals(updateType)) {  // 累加
            updateData1(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        }
	}
	
	// 更新结果-分时
	private void updateData(Map<Integer, OverviewDetailData> map, List<EntityNewUserAddCashUserByHour> ooLookDate, List<EntityNewUserAddCashUserByHour> ooCompareDate, List<EntityNewUserAddCashUserByHour> ooLastWeekDate) {
		for (EntityNewUserAddCashUserByHour o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewPayUserLookValue(o.getNew_user_addcash_user() + odd.getNewPayUserLookValue());
			}
		}
		
		for (EntityNewUserAddCashUserByHour o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewPayUserCompareValue(o.getNew_user_addcash_user() + odd.getNewPayUserCompareValue());
			}
		}

        for (EntityNewUserAddCashUserByHour o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setNewPayUserLastWeekValue(o.getNew_user_addcash_user() + odd.getNewPayUserLastWeekValue());
            }
        }
	}

    // 更新结果-累计
    private void updateData1(Map<Integer, OverviewDetailData> map, List<EntityNewUserAddCashUserByHour> ooLookDate, List<EntityNewUserAddCashUserByHour> ooCompareDate, List<EntityNewUserAddCashUserByHour> ooLastWeekDate) {
    	for (EntityNewUserAddCashUserByHour o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewPayUserLookValue(o.getNew_user_addcash_user() + odd.getNewPayUserLookValue());
			}
		}
		
		for (EntityNewUserAddCashUserByHour o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setNewPayUserCompareValue(o.getNew_user_addcash_user() + odd.getNewPayUserCompareValue());
			}
		}

        for (EntityNewUserAddCashUserByHour o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setNewPayUserLastWeekValue(o.getNew_user_addcash_user() + odd.getNewPayUserLastWeekValue());
            }
        }

        // 累计规则：三个日期中，与本日相同的那个的累计规则特殊，需要判断它的数据列表中最大的hour，这个hour之后的数据不累加且计为0
        String todayStr = Time.getDateStr(Time.currentDate());
        OverviewDetailData oneData = map.get(0);
        boolean isLookToday = todayStr.equals(oneData.getLookDateStr());
        int maxLookHour = (ooLookDate == null || ooLookDate.size() == 0) ? 0 : ooLookDate.get(ooLookDate.size() - 1).getHour();
        boolean isCompareToday = todayStr.equals(oneData.getCompareDateStr());
        int maxCompareHour = (ooCompareDate == null || ooCompareDate.size() == 0) ? 0 : ooCompareDate.get(ooCompareDate.size() - 1).getHour();
        boolean isLastWeekToday = todayStr.equals(oneData.getLastWeekDateStr());
        int maxLastWeekHour = (ooLastWeekDate == null || ooLastWeekDate.size() == 0) ? 0 : ooLastWeekDate.get(ooLastWeekDate.size() - 1).getHour();

        int sum1 = 0,sum2 = 0,sum3 = 0;
        //由于传入的map子类是LinkedHashMap且根据hour顺序初始化，故不再寻找最大hour从0开始累计 下同
		for(Map.Entry<Integer, OverviewDetailData> entry : map.entrySet()){
		    int hour = entry.getKey();

            sum1 += entry.getValue().getNewPayUserLookValue();
		    if (!isLookToday || hour <= maxLookHour) {
                entry.getValue().setNewPayUserLookValue(sum1);
            }
			sum2 += entry.getValue().getNewPayUserCompareValue();
		    if (!isCompareToday || hour <= maxCompareHour) {
                entry.getValue().setNewPayUserCompareValue(sum2);
            }
			sum3 += entry.getValue().getNewPayUserLastWeekValue();
			if (!isLastWeekToday || hour <= maxLastWeekHour) {
                entry.getValue().setNewPayUserLastWeekValue(sum3);
            }
		}
    }
}
